package shop.core.plugins.wxmsg;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import shop.core.common.oscache.SystemManager;
import shop.core.plugins.wxlogin.WxUrlType;
import shop.core.plugins.wxlogin.WxUtil;
import shop.core.util.KeyValueHelper;
import shop.services.front.activity.bean.Activity;
import shop.services.front.order.bean.Order;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description  微信发送模板消息
 * @Author semstouch
 * @Date 2017/7/16
 **/
public class WxMsgUtil {
    protected static final Logger logger = LoggerFactory.getLogger(WxMsgUtil.class);

    /**
     * 模板消息通知：付款成功
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendPayStatusMsg(Map<String, String> param, SystemManager systemManager, String openid,Order order) {
        String templateId = KeyValueHelper.get("sendPayStatusMsg").trim();   //消息模板编号
        logger.debug("消息模板编号："+templateId);
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/order/toOrderDetail?orderID="+order.getId());

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：待付款
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendUnPayStatusMsg(Map<String, String> param, SystemManager systemManager, String openid,Order order) {
        String templateId = KeyValueHelper.get("sendUnPayStatusMsg").trim();   //消息模板编号
        logger.debug("消息模板编号："+templateId);
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/order/toOrderDetail?orderID="+order.getId());

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：活动报名成功
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendActivityJoinStatusMsg(Map<String, String> param, SystemManager systemManager, String openid,Activity activity) {
        String templateId = KeyValueHelper.get("sendActivityJoinStatusMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/activity/toActivityDetail?activityID="+activity.getId());

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：股东申请
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendPartnerApplyStatusMsg(Map<String, String> param, SystemManager systemManager, String openid) {
        String templateId = KeyValueHelper.get("sendPartnerApplyStatusMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/partnerapply/toPartnerApply?partnerApplyID=0");

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：股东审核结果通知
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendPartnerCheckStatusMsg(Map<String, String> param, SystemManager systemManager, String openid) {
        String templateId = KeyValueHelper.get("sendPartnerCheckStatusMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/partnerapply/toPartnerApply?partnerApplyID=0");

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：会员加入
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendMemberJoinMsg(Map<String, String> param, SystemManager systemManager, String openid) {
        String templateId = KeyValueHelper.get("sendMemberJoinMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/user/toWx?myTeamTZ=0");

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：收益到账
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendIncomeMsg(Map<String, String> param, SystemManager systemManager, String openid) {
        String templateId = KeyValueHelper.get("sendIncomeMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/user/toWx?recordListTZ=0");

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }

    /**
     * 模板消息通知：下级解绑
     *
     * @param param       要发送的信息内容
     * @param systemManager 系统参数
     * @param openid      需要接收的微信号openid
     * @return 成功或者失败
     */
    public static boolean sendUnBindMsg(Map<String, String> param, SystemManager systemManager, String openid) {
        String templateId = KeyValueHelper.get("sendUnBindMsg").trim();   //消息模板编号
        Map params = new HashMap();
        params.put("touser", openid);
        params.put("template_id", templateId);
        params.put("url", systemManager.getSystemSetting().getWww()+"/rest/front/user/toWx?myTeamTZ=0");

        Map msgParam= new HashMap();
        //处理一下内容参数，将内容从String转成map格式
        for (Map.Entry<String, String> entry : param.entrySet()) {
            Map<String, String> keyMap = new HashMap();
            keyMap.put("value", entry.getValue());
            keyMap.put("color", "#173177");
            msgParam.put(entry.getKey(), keyMap);
        }

        Map<String, String> getParams = new HashMap<String, String>();
        getParams.put("access_token", systemManager.getAccessToken().getAccess_token());
        params.put("data", msgParam);
        String postData = "";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            postData = objectMapper.writeValueAsString(params);
        } catch (Exception e) {
            logger.error("", e);
        }
        try {
            WxRespCodeEntity wxRespCodeEntity = WxUtil.sendRequest(WxUrlType.msgTemplateUrl, HttpMethod.POST, getParams, new StringEntity(postData, "UTF-8"), WxRespCodeEntity.class);
            logger.info("发送模版信息结果：" + wxRespCodeEntity.getErrcode());
        } catch (Exception e) {
            logger.error("", e);
        }
        return true;
    }
}
